import { getUserInfo } from '@/api/api';
const user = {
  state: {
    roles: [],
    perms: []
  },

  mutations: {
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMS: (state, perms) => {
      state.perms = perms
    }
  },
  actions: {
      // 获取用户信息
      GetUserInfo({ commit }) {
          return new Promise((resolve, reject) => {
              getUserInfo().then(response => {
                  const data = response.data.data;

                  if (data.perms && data.perms.length > 0) { // 验证返回的perms是否是一个非空数组
                      commit('SET_PERMS', data.perms)
                  } else {
                      reject('getInfo: perms must be a non-null array !')
                  }

                  commit('SET_ROLES', data.roles);
                  resolve(response)
              }).catch(error => {
                  reject(error)
              })
          })
      }
  }
};

export default user
